package everyday.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @Author: wenjingyuan
 * @Date: 2022/10/12/10:28
 * @Description:链表组件
 * https://leetcode.cn/problems/linked-list-components/
 */
public class Num817 {
    public int numComponents(ListNode head, int[] nums) {
        int ans=0;
        Set<Integer> set=new HashSet<Integer>();
        for(int x:nums){
            set.add(x);
        }
        while (head!=null){
            if(set.contains(head.val)){
                while (head != null && set.contains(head.val)) {head = head.next;}
                ans++;
            }else{
                head=head.next;
            }
        }
        return ans;
    }
}
